package cn.itsource.ymcc.listenner;

import cn.itsource.ymcc.constants.BaseConstants;
import cn.itsource.ymcc.domain.Login;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
@Slf4j
public class AuthenticationSuccessListenner implements ApplicationListener<AuthenticationSuccessEvent> {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent authenticationSuccessEvent) {

        Authentication authentication = authenticationSuccessEvent.getAuthentication();

        UserDetails userDetails = (UserDetails) authentication.getPrincipal();

        String username = userDetails.getUsername();

        try{
            Login login = JSON.parseObject(username, Login.class);

            //加载用户的购物车
            loadCar2Redis(login);
        }catch (Exception e){
        }
    }

    public void loadCar2Redis(Login login){
        log.info("用户认证成功,加载初始数据："+login);
        //rocketMQTemplate.sendOneWay(BaseConstants.Cart.TOPIC_CART_2REDIS+":"+BaseConstants.Cart.TAGS_CART_2REDIS,login.getId());
    }
}
